package com.jweb.framework.core.support;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class UtilA{ 
	
	private static final Pattern a = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}");
    private final Pattern b;
    private final List c = new LinkedList();

	UtilA(String s)
    {
        b = a(s);
    }

    private Pattern a(String s)
    {
        StringBuilder stringbuilder = new StringBuilder();
        Matcher matcher = a.matcher(s);
        int i;
        for(i = 0; matcher.find(); i = matcher.end())
        {
            stringbuilder.append(a(s, i, matcher.start()));
            String s1 = matcher.group();
            if("?".equals(s1))
                stringbuilder.append('.');
            else
            if("*".equals(s1))
                stringbuilder.append(".*");
            else
            if(s1.startsWith("{") && s1.endsWith("}"))
            {
                int j = s1.indexOf(':');
                if(j == -1)
                {
                    stringbuilder.append("(.*)");
                    c.add(matcher.group(1));
                } else
                {
                    String s2 = s1.substring(j + 1, s1.length() - 1);
                    stringbuilder.append('(');
                    stringbuilder.append(s2);
                    stringbuilder.append(')');
                    String s3 = s1.substring(1, j);
                    c.add(s3);
                }
            }
        }

        stringbuilder.append(a(s, i, s.length()));
        return Pattern.compile(stringbuilder.toString());
    }

    private String a(String s, int i, int j)
    {
        if(i == j)
            return "";
        else
            return Pattern.quote(s.substring(i, j));
    }

    public boolean a(String s, Map map)
    {
        Matcher matcher = b.matcher(s);
        if(matcher.matches())
        {
            if(map != null)
            {
                for(int i = 1; i <= matcher.groupCount(); i++)
                {
                    String s1 = (String)c.get(i - 1);
                    String s2 = matcher.group(i);
                    map.put(s1, s2);
                }
            }
            return true;
        } else
        {
            return false;
        }
    }

}
